extends Node2D

@onready var parent = get_parent()

const speed : float = Globals.tile_size * 9
const jump_height : float = Globals.tile_size * 2.25
const jump_velocity : float = -sqrt(jump_height * 2 * Globals.gravity)

func _ready() -> void:
	EventBus.player_moving_to_box.connect(move_to_box)

func _physics_process(delta: float) -> void:
	if Input.is_action_just_pressed("player_jump") and parent.is_on_floor():
		parent.velocity.y = jump_velocity

	var direction := Input.get_axis("player_left", "player_right")
	if direction:
		parent.velocity.x = direction * speed
		parent.getting_force = true
	else:
		parent.getting_force = false

func delete_from_parent() -> void:
	get_parent().remove_child(self)
	parent.velocity.x = 0
	
func move_to_box(box : Box) -> void:
	delete_from_parent()
	box.add_child(self)
	self.parent = box
